import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'package:flutter_ncsjqz_getx/model/user_detail_entity.dart';
import 'package:flutter_ncsjqz_getx/page/my/email_phone_pwd/email_password_view.dart';
import 'package:flutter_ncsjqz_getx/widget/app_bar_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/base_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/bottom_sheet_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/row_line_widget.dart';
import 'package:get/get.dart';

import 'set_name/set_nike_name_view.dart';
import 'set_sex/set_sex_view.dart';
import 'user_info_setting_logic.dart';

/// <summary>
/// todo: 用户信息设置
/// author：zwb
/// dateTime： 2021/11/6 17:34
/// filePath： lib/page/my/userinfo_setting/user_info_setting_view.dart
/// desc:
/// <summary>
///
class UserInfoSettingPage extends StatefulWidget {
  final UserDetailData? userDetailData;
  UserInfoSettingPage({this.userDetailData});
  @override
  _UserInfoSettingPageState createState() => _UserInfoSettingPageState();
}

class _UserInfoSettingPageState extends State<UserInfoSettingPage> {
  final logic = Get.put(UserInfoSettingLogic());

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    logic.userDetailData = widget.userDetailData;
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBarWidget(
        title: "个人资料",
      ),
      backgroundColor: Colors.white,
      body: GetBuilder<UserInfoSettingLogic>(
        builder: (controller) {
          return baseScaffoldWidget(
            loading: logic.loading,
            child: ListView(
              padding: EdgeInsets.symmetric(
                horizontal: 15,
              ),
              children: [
                rowBaseWidget(
                  leftText: "头像",
                  right: Row(
                    children: [
                      InkWidget.imgNet(
                        "${logic.userDetailData?.userPic}",
                        width: 40,
                        height: 40,
                        fit: BoxFit.cover,
                        borderRadius: BorderRadius.circular(10),
                      ),
                      Icon(
                        Icons.chevron_right,
                        color: Colors.grey.shade400,
                      ),
                    ],
                  ),
                  onTap: () async {
                    BottomSheetWidget.bottomImagePickWidget(
                      callBack: (v) async{
                        Get.back();
                        if(v.length>0){
                          logic.start();
                          await logic.uploadFile(v[0].path).catchError((e)=> logic.close());
                          Get.put(UserInfoSettingLogic()).setUserInfo({"id":logic.userDetailData!.id,"userPic":logic.userDetailData!.userPic});
                          logic.close();
                        }
                      },
                    );
                  },
                ),
                rowBaseWidget(
                  leftText: "昵称",
                  rightText: "${logic.userDetailData?.nickName ?? ''}",
                  onTap: () {
                    print(logic.userDetailData!.id);
                    Get.to(() => SetNikeNamePage(title: "昵称",type: SNM.nikeName,value: "${logic.userDetailData?.nickName}",),)!.then((value){
                      logic.userDetailData!.nickName = value;
                      logic.update();
                    });
                  },
                ),
                rowBaseWidget(
                  leftText: "地区",
                  rightText: "${logic.userDetailData?.location ?? ''}",
                  onTap: () async{
                    await CityPickers.showCityPicker(
                      context: context,
                      height: 280,
                      confirmWidget: Text("确定"),
                      cancelWidget: Text("取消"),
                      barrierDismissible: false,
                    ).then((value){
                      String? loc = value!.provinceName! + value.cityName! + value.areaName!;
                      Get.put(UserInfoSettingLogic()).setUserInfo({"id":logic.userDetailData!.id,"location":loc});
                      logic.userDetailData!.location = loc;
                      logic.update();
                    });
                  },
                ),
                rowBaseWidget(
                  leftText: "性别",
                  rightText: "${logic.userDetailData?.sex==0?'未设置':logic.userDetailData?.sex==1?'男':'女'}",
                  onTap: () {
                    Get.to(() => SetSexPage(logic.userDetailData?.sex==0?-1:logic.userDetailData?.sex),)!.then((value){
                      logic.userDetailData!.sex = value;
                      logic.update();
                    });
                  },
                ),
                rowBaseWidget(
                  leftText: "出生年月",
                  rightText: "${logic.userDetailData?.birth ?? ''}",
                  onTap: (){
                    DatePicker.showDatePicker(
                      context,
                      minDateTime: DateTime.parse("1900-01-01"),
                      maxDateTime: DateTime.now(),
                      initialDateTime: DateTime.now(),
                      dateFormat: 'yyyy年-M月-d日',
                      locale: DateTimePickerLocale.zh_cn,
                      pickerTheme: DateTimePickerTheme(
                        showTitle: true,
                        confirmTextStyle: TextStyle(color: Colors.green)
                      ),
                      pickerMode: DateTimePickerMode.date,
                      onCancel: () {
                        debugPrint('onCancel');
                      },
                      onChange: (dateTime, List<int> index) {
                        print(dateTime);
                      },
                      onConfirm: (dateTime, List<int> index) {
                        String? loc = "${dateTime.year}-${dateTime.month}-${dateTime.day}";
                        Get.put(UserInfoSettingLogic()).setUserInfo({"id":logic.userDetailData!.id,"birth":loc});
                        logic.userDetailData!.birth = loc;
                        logic.update();
                      },
                    );
                  }
                ),
                rowBaseWidget(
                  leftText: "手机号",
                  rightText: "${logic.userDetailData?.telPhone ?? ''}",
                  onTap: () {
                    Get.to(()=>EmailPhonePasswordPage(title: "设置手机号",type: EPW.phone,emailPhone: "${logic.userDetailData?.telPhone ?? ''}",),);
                  },
                ),
                rowBaseWidget(
                  leftText: "兴趣爱好",
                  rightText: "${logic.userDetailData?.hobby ?? ''}",
                  onTap: () {
                    Get.to(() => SetNikeNamePage(title: "兴趣爱好",type: SNM.loveGood,value: "${logic.userDetailData?.hobby ?? ''}",),arguments:{"id":logic.userDetailData!.id})!.then((value){
                      logic.userDetailData!.hobby = value;
                      logic.update();
                    });
                  },
                ),
                rowBaseWidget(
                  leftText: "个人介绍",
                  showLine: false,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  padding: EdgeInsets.only(top: 10),
                  rightFlex: 2,
                  rightText: "${logic.userDetailData?.selfIntroduction ?? ''}",
                  onTap: () {
                    Get.to(() => SetNikeNamePage(title: "个人介绍",type: SNM.brief,value: "${logic.userDetailData?.selfIntroduction ??''}",),)!.then((value){
                      logic.userDetailData!.selfIntroduction = value;
                      logic.update();
                    });
                  },
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<UserInfoSettingLogic>();
    super.dispose();
  }
}
